<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :tree-node="companyInfo" @on-add="add" />

        <!-- 树形结构 -->
        <el-tree :data="treeData" :default-expand-all="true">
          <tree-tools slot-scope="{ data }" :tree-node="data" @on-delete="getDepartment" @on-add="add" />
        </el-tree>
      </el-card>
    </div>

    <!-- 添加对话框   给父组件添加.sync 父向子传值 -->
    <add-depts :show-dialog.sync="showDialog" :tree-node="node" />
  </div>
</template>

<script>
// 导入接口
import { getDepartment } from '@/api/department'
import { listToTree } from '@/utils'
import treeTools from './components/tree-tools.vue'
import AddDepts from './components/add-depts.vue'
export default {
  name: 'Departments',
  components: { treeTools, AddDepts },
  data() {
    return {
      // 树形图数据
      treeData: [],
      companyInfo: {
        name: '', manager: '负责人', root: true
      },
      showDialog: false,
      node: null
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 对话框监听事件
    add(node) {
      console.log(node)
      this.node = node
      this.showDialog = true
    },
    // 获取组织架构数据
    async getDepartment() {
      const res = await getDepartment()
      // console.log(res)
      this.companyInfo.name = res.companyName
      this.treeData = listToTree(res.depts, '')
      this.companyInfo.children = this.treeData
    }

  }
}
</script>

<style scoped lang="scss">
.tree-card {
  font-size:14px;
}
.el-row{
  width: 100%;
}
</style>
